<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends Controller {

    /**
     * @param int $id 商品分类id
     */
    public function index(){
        $News = M('News');//实例化AdminUser对象
        $count = $News->count();// 查询满足要求的总记录数
        $itemNum = 4; //每页显示的记录数
        $show = h_page($count,'条新闻');
        //page方法分页
        $data = $News
            ->field(array('id','title','content','u_time'))
            ->order('u_time desc, id desc')
            ->page(I('get.p',1) . ',' . $itemNum)
            ->select();
        foreach ($data as $k => $v){
            $data[$k]['content'] = subtext(strip_tags($v['content']),40,'...');
        }

        if(IS_AJAX){
            $json['news_list'] = $data;
            $json['page'] = $show;
            $this->ajaxReturn($json);
        }else{
            $center = M('NewsCenter')->order('u_time desc')->find();
            $this->assign('news_list',$data);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->assign('center',$center);// 中心输出
            $this->display();
        }
    }

    public function info($id = 0){
        $one = M('News')->find($id);
        if(!$one){
            $this->error("参数错误");
        }
//        SELECT id FROM lh_goods WHERE u_time >= 1496907927 AND id != 33 LIMIT 0,1;
        $prev = M('news')
            ->field('id,title')
            ->where(' u_time >= ' . $one['u_time'] . ' and '. ' id != ' . $one['id'])
                ->order('u_time asc','id desc')
                ->limit(0,1)
                ->select();
//        dump(M('news')->getLastSql());
        $next = M('news')
            ->field('id,title')
            ->where(' u_time <= ' . $one['u_time'] . ' and '. ' id != ' . $one['id'])
            ->order('u_time desc','id asc')
            ->limit(0,1)
            ->select();
//        dump($next);
//        dump($prev);
        $this->assign('one',$one);
        $this->assign('prev',$prev);
        $this->assign('next',$next);
        $this->display();
    }


}